package com.rzico.sbl.ui.report.dispatch

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import com.rzico.sbl.R
import com.rzico.sbl.databinding.FragmentDispatchFirstBinding
import com.rzico.sbl.model.DispatchBucket
import com.rzico.sbl.viewmodel.ReportDispatchViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.base.NavigationFragment
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeByFinally
import com.xinnuo.slimadapter.SlimAdapter

class DispatchFirstFragment
@JvmOverloads
constructor(
    private val shipId: String = ""
) : NavigationFragment() {

    private var mBinding: FragmentDispatchFirstBinding? = null

    override fun getViewModel() = get<ReportDispatchViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FragmentDispatchFirstBinding
        .inflate(inflater, container, false)
        .also { mBinding = it }
        .root

    override fun initTitle() {
        initLayout()

        mBinding?.let {
            it.listLayout
                .swipeRefresh
                .isRefreshing = true

            getData()
        }
    }

    private fun initLayout() {
        mBinding?.let { binding ->
            binding.listLayout.apply {
                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData() }
                    )

                    layout.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                    mAdapter = SlimAdapter.creator()
                        .register<DispatchBucket>(R.layout.item_report_dispatch_bucket) { jector, bean, _ ->
                            jector.apply {
                                gone(R.id.item_bucket_edit)
                                text(R.id.item_bucket_barrel, bean.name)

                                // 送出桶
                                with<EditText>(R.id.item_bucket_out) {
                                    it.isEnabled = false
                                    it.setText(bean.quantity.orEmpty("0"))
                                }

                                // 以往欠桶
                                with<EditText>(R.id.item_bucket_old) {
                                    it.isEnabled = false
                                    it.setText(bean.borrow.orEmpty("0"))
                                }

                                // 入库
                                with<EditText>(R.id.item_bucket_in) {
                                    it.isEnabled = false
                                    it.setText(bean.returnQuantity.orEmpty("0"))
                                }

                                // 司机还桶
                                with<EditText>(R.id.item_bucket_driver) {
                                    it.isEnabled = false
                                    it.setText(bean.backQuantity.orEmpty("0"))
                                }

                                text(R.id.item_bucket_qian, bean.arrearsQuantity.orEmpty("0")) //欠桶
                                text(R.id.item_bucket_huan, bean.repayQuantity.orEmpty("0"))   //还桶

                                // 押桶
                                text(R.id.item_bucket_ya, bean.pledgeQuantity)
                                text(R.id.item_bucket_money2, bean.pledge.formatDecimal())
                                // 买桶
                                text(R.id.item_bucket_mai, bean.buyQuantity)
                                text(R.id.item_bucket_money3, bean.buy.formatDecimal())
                                // 退桶
                                text(R.id.item_bucket_tui, bean.refundsQuantity)
                                text(R.id.item_bucket_money4, bean.refunds.formatDecimal())

                                // 兑换桶
                                text(R.id.item_bucket_change, bean.transQuantity.orEmpty("0"))
                                // 累计欠桶
                                text(
                                    R.id.item_bucket_new,
                                    (bean.borrow.intValue
                                            - bean.adAdvanceStock.intValue
                                            + bean.backQuantity.intValue
                                            + bean.arrearsQuantity.intValue
                                            - bean.repayQuantity.intValue).toString()
                                )

                                // 预存桶抵扣
                                text(R.id.item_bucket_store, bean.adAdvanceStock)

                                text(R.id.item_bucket_one, bean.percent1.orEmpty("0"))   //外品牌1:1
                                text(R.id.item_bucket_two, bean.percent2.orEmpty("0"))   //外品牌2:1
                                text(R.id.item_bucket_three, bean.percent3.orEmpty("0")) //外品牌3:1
                                text(R.id.item_bucket_fill, bean.percent4.orEmpty("0"))  //外品牌补差价
                                text(R.id.item_bucket_money1, bean.differPrice.formatDecimal())  //补差价金额

                                text(R.id.item_bucket_memo, bean.memo1) //备注

                                visibility(
                                    R.id.item_bucket_ya_ll,
                                    visibleValue { bean.pledgeQuantity.intValue > 0 }
                                )

                                visibility(
                                    R.id.item_bucket_mai_ll,
                                    visibleValue { bean.buyQuantity.intValue > 0 }
                                )

                                visibility(
                                    R.id.item_bucket_tui_ll,
                                    visibleValue { bean.refundsQuantity.intValue > 0 }
                                )

                                visibility(
                                    R.id.item_bucket_store_ll,
                                    visibleValue { bean.adAdvanceStock.intValue > 0 }
                                )

                                visibility(
                                    R.id.item_bucket_other_ll,
                                    visibleValue {
                                        bean.percent1.intValue > 0
                                                || bean.percent2.intValue > 0
                                                || bean.percent3.intValue > 0
                                                || bean.percent4.intValue > 0
                                    }
                                )

                                visibility(
                                    R.id.item_bucket_memo_ll,
                                    goneValue { bean.memo1.isNullOrEmpty() }
                                )
                            }
                        }
                        .attachTo(layout)
                }
            }
        }
    }

    /**
     * 空桶列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .getBarrelList(
                itemId = shipId,
                isLoading = false
            )
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = { value ->
                    mList.clear()
                    mList.addItems(value.filter { it.show })
                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding?.let {
                        it.listLayout
                            .swipeRefresh
                            .isRefreshing = false
                    }
                }
            )
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }

    companion object {
        fun newInstance(shipId: String) = DispatchFirstFragment(shipId)
    }

}